package com.zxy.javaarithmetic.chapter_1_StackQueue;

import java.util.Stack;

/*
 *  @项目名：  study
 *  @包名：    com.zxy.javaarithmetic.chapter_1_StackQueue
 *  @文件名:   P_2_TwoStackImplementQueue
 *  @创建者:   zhangxy
 *  @创建时间:  2018/12/3 10:59
 *  @描述：    TODO
 */
public class P_2_TwoStackImplementQueue {

	public static void main(String[] args){
	    MyQueue<Integer> myQueue = new MyQueue<>();
		for (int i = 0; i < 5; i++) {
			myQueue.add((int) (Math.random()* 100));
		}
		System.out.print(myQueue.poll()+" ");
		System.out.println(""+myQueue.poll());

		for (int i = 0; i < 5; i++) {
			myQueue.add((int) (Math.random()* 100));
		}

		while (!myQueue.isEmpty()){
			System.out.print(myQueue.poll()+"   ");
		}
	}

	public static class MyQueue<T> {

		private Stack<T> pushStack;
		private Stack<T> popStack;

		public MyQueue() {
			pushStack = new Stack<>();
			popStack = new Stack<>();
		}

		public void add(T t) {
			pushStack.push(t);
		}

		public T poll() {
			if (popStack.isEmpty() && pushStack.isEmpty()) {
				throw new RuntimeException("this queue is empty");
			} else if (popStack.isEmpty()) {
				while (!pushStack.isEmpty()) {
					popStack.push(pushStack.pop());
				}
			}
			return popStack.pop();
		}


		public T peek() {
			if (popStack.isEmpty() && pushStack.isEmpty()) {
				throw new RuntimeException("this queue is empty");
			} else if (popStack.isEmpty()) {
				while (!pushStack.isEmpty()) {
					popStack.push(pushStack.pop());
				}
			}
			return popStack.peek();
		}

		public boolean isEmpty(){
			return popStack.isEmpty() && pushStack.isEmpty();
		}
	}
}
